using HMCollada.Core;
using System.Collections.ObjectModel;
using System.Xml;

namespace HMCollada.Core {
    internal struct HMColladaMesh : HMColladaGeometryType {
        private Collection<HMColladaSource> source;
        private HMColladaVertices vertices;
        private Collection<HMColladaPolygons> polygons;
        private Collection<HMColladaPolylist> polylist;
        private Collection<HMColladaTriangles> triangles;

        public Collection<HMColladaSource> Source { get { return source; } set { source = value; } }
        public HMColladaVertices Vertices { get { return vertices; } set { vertices = value; } }
        public Collection<HMColladaPolygons> Polygons { get { return polygons; } set { polygons = value; } }
        public Collection<HMColladaPolylist> Polylist { get { return polylist; } set { polylist = value; } }
        public Collection<HMColladaTriangles> Triangles { get { return triangles; } set { triangles = value; } }
    }

    internal interface HMColladaGeometryType {
        Collection<HMColladaSource> Source { get; set; }
        HMColladaVertices Vertices { get; set; }
        Collection<HMColladaPolygons> Polygons { get; set; }
        Collection<HMColladaPolylist> Polylist { get; set; }
        Collection<HMColladaTriangles> Triangles { get; set; }
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private static HMColladaGeometryType LoadMesh() {
            HMColladaMesh newMesh = new HMColladaMesh();

            newMesh.Source = new Collection<HMColladaSource>();
            newMesh.Polygons = new Collection<HMColladaPolygons>();
            newMesh.Polylist = new Collection<HMColladaPolylist>();
            newMesh.Triangles = new Collection<HMColladaTriangles>();

            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "source": newMesh.Source.Add(LoadSource()); break;
                        case "vertices": newMesh.Vertices = LoadVertices(); break;
                        case "polygons": newMesh.Polygons.Add(LoadPolygons()); break;
                        case "polylist": newMesh.Polylist.Add(LoadPolylist()); break;
                        case "triangles": newMesh.Triangles.Add(LoadTriangles()); break;
                        case "lines": colladaReader.Read(); break; // skip these ones
                        case "linestrips": colladaReader.Read(); break;
                        case "trifans": colladaReader.Read(); break;
                        case "tristrips": colladaReader.Read(); break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "mesh");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newMesh;
        }
    }
}